package com.personnalManagerSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.personnalManagerSystem.model.Attend;
import com.personnalManagerSystem.model.User;
import com.personnalManagerSystem.service.IAttendService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liqing
 * @since 2022-03-27
 */
@Controller
@RequestMapping("/attend")
public class AttendController  extends  BaseController{
    public Model baseList(Model model,
                          String option,
                          String searchContent,
                          Integer pageNum,
                          Integer pageSize,
                          IAttendService iAttendService
                         ){
        //设置默认的页码
        if(pageNum ==null || pageNum==0){
            pageNum=1;
        }
        //设置一页默认的显示记录
        if(pageSize==null  || pageSize==0){
            pageSize=5;
        }
        //创建一个构造器
        QueryWrapper<Attend> wrapper = new QueryWrapper<>();
        if(option!=null
                && searchContent!=null
                && !"".equals(option)
                && !"".equals(searchContent)){
            wrapper.like(option,searchContent);
        }

        //设置分页
        Page<List<Attend>> pageInfo = PageHelper.startPage(pageNum, pageSize);
        //在登录的时候将用户记录存储在了session中
        User user =(User) SecurityUtils.getSubject().getSession().getAttribute("user");
        if("普通用户".equals(user.getDescription())){
            //如果其角色是普通用户,那么只可以查询关于自己的一系列消息,不能出现跨用户访问,否则会出现资源泄露
            wrapper.eq("user_id",user.getUserId());
        }
        //查询数据
        iAttendService.selectAttendItem(wrapper);
        //设置页面信息
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("option",option);
        model.addAttribute("searchContent",searchContent);
        return model;
    }
    @RequestMapping("/list")
    public String list(Model model,
                       String option,
                       String searchContent,
                       Integer pageNum,
                       Integer pageSize){
        baseList(model,option,searchContent,pageNum,pageSize,iAttendService);
        return "view/attend";
    }

    @RequestMapping("/add")
    /**
     * @Description 上班打卡
     * @author liqing
     * @data 2022/4/4  21:29
     * @param userId
     * @param attendTime
     * @param workTime
     * @return java.lang.String
     */
    public String add(@RequestParam(value ="userId",required = true)Integer userId,
                      /*@RequestParam(value = "attendTime",required = false)String attendTime,*/
                      @RequestParam(value = "workTime",required = false)String workTime1){
        //如果userId不存在,那么就跳转到全查页面,因为不排除会Id错误
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        User oneUser = iUserService.getOne(wrapper);
        if(oneUser==null){
            return "redirect:/attend/list";
        }
        //出勤日期就是今天所在的日期
        LocalDate attendTime = LocalDate.now();
        System.out.println("attendTime:"+attendTime);
        Attend attend = new Attend();
        //获取上班打卡的时间
        LocalDateTime workTime = LocalDateTime.parse(workTime1);
        if(attendTime.getDayOfMonth()!=workTime.getDayOfMonth()){
            //如果考勤日期和打卡日期不相符合,则跳转到全查页面
            return "redirect:/attend/list";
        }
        int hour = workTime.getHour();
        System.out.println("hour:"+hour);
        /**
         * 上班状态,上班状态正常为1,迟到为0,早退为1,既迟到又早退的状态设置为-2上班的时间为早九晚六
         */
        if(hour>9){
            //说明已经迟到了,设置考勤状态为0
            attend.setAttendState("0");
            attend.setResult("迟到");
        }else{
            //先默认为正常,在下班打卡时如果早退就重新设置考勤状态,如果没有就默认为该状态为正常.
            attend.setAttendState("1");
            attend.setResult("正常");
        }

        attend.setAttendTime(attendTime);
        attend.setUserId(userId);
        attend.setWorkTime(workTime);
        iAttendService.save(attend);
        return "redirect:/attend/list";
    }
    @RequestMapping("/edit")
    /**
     * @Description  下班打卡
     * @author liqing
     * @data 2022/4/4  22:18
     * @param attendId
     * @param quitTime
     * @return java.lang.String
     */
    public String edit(@RequestParam(value = "userId",required = false)Integer userId,
                       @RequestParam(value = "attendId",required = true)Integer attendId,
                       @RequestParam(value = "quitTime",required = false)String quitTime1){
       /* System.out.println("userId:"+userId);
        System.out.println("attendId:"+attendId);*/
        LocalDateTime quitTime = LocalDateTime.parse(quitTime1);
        //出勤日期就是今天所在的日期
        LocalDate attendTime = LocalDate.now();
        if(attendTime.getDayOfMonth()!=quitTime.getDayOfMonth()){
            return "redirect:/attend/list";
        }
        Attend attend = iAttendService.getById(attendId);
        attend.setAttendId(attendId);
        attend.setUserId(userId);
        attend.setQuitTime(quitTime);
        String attendState = attend.getAttendState();
        int hour = quitTime.getHour();
        if(hour<18){
            //早退
            attend.setResult("早退");
            attend.setAttendState("-1");

        }
        if("0".equals(attendState) && hour<18){
            //已经迟到
            attend.setAttendState("-2");
            attend.setResult("迟到+早退");
        }
        //iAttendService.updateById(attend);
        return "redirect:/attend/list";
    }

    @RequestMapping("/del")
    public String del(@RequestParam(value = "attendId",required = true)Integer attendId){
        System.out.println("attendId:"+attendId);
        iAttendService.removeById(attendId);
        return "redirect:/attend/list";
    }
}

